package com.example.demo.service.impl;

import com.example.demo.entity.RandomName;
import com.example.demo.mapper.NameMapper;
import com.example.demo.service.NameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class NameServiceImpl implements NameService {

    @Autowired
    private NameMapper nameMapper;

    @Override
    public void saveNamesFromFile(String filePath) throws IOException {
        List<RandomName> names = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] nameArray = line.split("、");
                for (String name : nameArray) {
                    RandomName randomName = new RandomName();
                    randomName.setName(name);
                    names.add(randomName);
                }
            }
        }
        nameMapper.batchInsertNames(names);
    }

    @Override
    public String getRandomName() {
        // 随机获取姓名
        RandomName nameObject = nameMapper.getName();
        // 可使用次数-1
        int times = nameObject.getTimes() - 1;
        // 如果次数 -1 后为0 则删除该条数据
        if (times == 0) {
            nameMapper.updateDisable(nameObject.getId());
        } else {
            // 将该条数据可使用次数 -1
            nameMapper.updateTimes(nameObject.getId());
        }
        return nameObject.getName();
    }

    @Override
    public String formatImageNames() {
        int count = 1;
        // 将所有图片名称格式化
        try {
            File folder = new File("D:\\Desktop\\图片\\聊天记录\\public\\images\\bg");
            if (!folder.exists() || !folder.isDirectory()) {
                System.out.println("文件夹不存在或路径错误");
                return "Error";
            }
            // 获取每个文件并排序，避免因文件列表顺序不同导致重命名混乱
            File[] files = folder.listFiles();
            if (files != null) {
                Arrays.sort(files); // 排序确保顺序一致
                for (File file : files) {
                    if (file.isFile()) {
                        String newFileName = count + ".png";
                        File newFile = new File(folder, newFileName);
                        if (!file.renameTo(newFile)) {
                            System.out.println("Failed to rename file: " + file.getName());
                        }
                        count++;
                    }
                }
            }
        } catch (SecurityException e) {
            System.err.println("没有权限操作文件或目录");
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "OK";
    }

    @Override
    public String resetAllNames() {
        try {
            nameMapper.resetCanUseTimes();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "OK";
    }
}    